package com.iplus.ydkf.ehome.module.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.avos.avoscloud.AVException;
import com.iplus.ydkf.ehome.common.utils.LeanErrorCode;
import com.iplus.ydkf.ehome.config.ConString;

import butterknife.ButterKnife;

/**
 * Created by liuhaitian on 16/4/15.
 */
public abstract class BaseFragment extends Fragment implements IBaseUi {
    protected boolean isPrepared;
    protected boolean isVisible;
    private View rootView;

    protected void logv(String str) {
        if (ConString.LOGGING)
            Log.v(getClass().getSimpleName(), str);
    }

    protected void loge(String str) {
        if (ConString.LOGGING) Log.e(getClass().getSimpleName(), str);
    }

    protected void logd(String str) {
        if (ConString.LOGGING) Log.d(getClass().getSimpleName(), str);
    }

    protected void logi(String str) {
        if (ConString.LOGGING) Log.i(getClass().getSimpleName(), str);
    }

    protected void toast(String str) {
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }

    protected void snackbar(String str) {
        Snackbar.make(rootView, str, Snackbar.LENGTH_SHORT).show();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(bindLayout(), null);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initData(view);
        isPrepared = true;
        lazyLoad();
    }

    public boolean isNoException(AVException e) {
        if (e == null) return true;
        else {
            try {
                JSONObject errorObject = JSONObject.parseObject(e.getMessage());
                if (TextUtils.isEmpty(errorObject.getString("error")))
                    LeanErrorCode.getErrorMessage(errorObject.getIntValue("code"));
                else snackbar(errorObject.getString("error"));
            } catch (Exception e1) {
                LeanErrorCode.getErrorMessage(e.getCode());
            }
            return true;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    protected void onVisible() {
        lazyLoad();
    }

    protected abstract void lazyLoad();

    protected void onInvisible() {
    }
}
